<?php

namespace App\Http\Controllers\Admin;

use App\Models\Goods;
use Illuminate\Http\Request;

use App\Http\Requests;
use DB;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{

    /**
     *  加载商品列表的方法
     */
    public function getIndex(Request $request)
    {
        $num = $request->num ?? 10;
        $numUrl = '&num='.$num;
        $keywords = $request->keywords? '&keywords='.$request->keywords : '';

        // 定义选择查询的条件 默认选择正在销售的商品
        $queryList = [
            'is_sale' => 1
        ];

        $hotUrl= $hot= '';
        if($request->hot != ''){
            $hotUrl = $request->hot? '&hot='.$request->hot : '';
            $hot = $queryList['is_hot'] = $request->hot;
        }

        $bestUrl= $best= '';
        if($request->best != ''){
            $bestUrl = $request->best? '&best='.$request->best : '';
            $best = $queryList['is_best'] = $request->best;
        }

        $newsUrl= $news= '';
        if($request->news != ''){
            $newsUrl = $request->news? '&news='.$request->news : '';
            $news = $queryList['is_new'] = $request->news;
        }



        if($request->keywords){
            //加载用户列表模板
            $goods = Goods::where('goods_name', 'like', '%'.$request->keywords.'%')
                    ->where($queryList)
                    ->paginate($num);
        }else{
            $goods = Goods::where($queryList)->paginate($num);
        }

        return view('Admin/Goods/index', [
            'goods' => $goods,
            'num'=>$num,
            'request'=>$request,
            'keywords'=>$keywords,
            'numUrl'=>$numUrl,
            'hotUrl'=>$hotUrl,
            'hot'=>$hot,
            'bestUrl'=>$bestUrl,
            'best'=>$best,
            'newsUrl'=>$newsUrl,
            'news'=>$news,
        ]);
    }

    /**
     *  加载添加商品表单的方法
     */
    public function getAdd()
    {
        $cates=CatesController::getCates();
        return view('Admin/Goods/add',['cates'=>$cates]);
    }

    /**
     *  执行商品添加的方法
     */
    public function postInsert(request $request){
        $data=$request->except('_token');
        $data['goods_color']=implode(',', $data['goods_color']);
        $data['goods_size']=implode(',', $data['goods_size']);
        $data['created_at'] = $data['updated_at'] = date('Y-m-d H:i:s', time());

        // dd($data);
        if($request->hasFile('goods_img')){

            if(($request)->file('goods_img')->isValid()){

                //接收到上传文件的后缀名
                $ext=$request->file('goods_img')->getClientOriginalExtension();

                //随机一个文件名
                $picname=date('YmdHis').'.'.$ext;

                //接收文件信息
                $result=$request->file('goods_img')->move('./uploads/',$picname);
                $data['goods_img']=$picname;
                //将信息添加到数据库
                $res=Goods::insert($data);
            }else{
                return back()->with('error','不是一个有效文件');
            }
        }else{
            $res=Goods::insert($data);
        }
        if($res){
            //返回用户列表页
            return redirect('/admin/goods')->with('success','添加成功');
        }else{
            return back();
        }
    }


    /**
     *  加载修改商品表单的方法
     */
    public function getEdit($id){
        $cates=CatesController::getCates();
        $data=Goods::where('id',$id)->first()->toArray();
        $size=$data['goods_size']=explode(',', $data['goods_size']);
        // dd($size);
        // $res=DB::select('select * from articles a,cates c where a.cate_id=c.id &&a.cate_id=',$id);
        $res=DB::table('goods')->join('cates','goods.cate_id','=','cates.id')->select('goods.*','cates.name')->get();
        // dd($res);
        return view('Admin/Goods/edit',['data'=>$data,'res'=>$res,'cates'=>$cates,'size'=>$size]);
    }

    /**
     *  执行商品修改的方法
     */
    public function postUpdate(Request $request){
        $id=$request->id;
        // dd($id);
        $info=$request->except(['_token','id']);
        // dd($info);
        $info['goods_color']=implode(',', $info['goods_color']);
        $info['goods_size']=implode(',', $info['goods_size']);
        //


        if($request->hasFile('goods_img')){

            //删除之前的图片
            $oldpic=DB::select('select * from articles where id=?',[$id])[0]['goods_img'];
            // dd($oldpic);
            //执行删除
            @unlink('./uploads/',$oldpic);

            //接收到上传文件的后缀名
            $ext=$request->file('goods_img')->getClientOriginalExtension();

            //随机一个文件名
            $picname=date('YmdHis').'.'.$ext;

            //接收文件信息
            $result=$request->file('goods_img')->move('./uploads/',$picname);
            $info['goods_img']=$picname;

            //      //将信息添加到数据库
            $res=Goods::where('id',$id)->update($info);
        }
        else{
            $res=Goods::where('id',$id)->update($info);
        }
        if($res){
            return redirect('admin/goods/index')->with('success','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }

    /**
     *  执行商品删除的方法
     */
    public function getDelete($id)
    {
        $res = Goods::destroy($id);
        if($res){
            return redirect('admin/goods/index')->with('success','删除成功');
        }else{
            return back()->with('error','删除失败');
        }
    }

}
